﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using PPI.Client.Logic.Inspection;
using PPI.Client.Logic.XML;
using PPI.Client.Utils;
using PPI.Entities.Enumerator;
using PPI.Entities.Inspections;
using PPI.Entities.XML;
using PPI.General;

namespace PPI.Client.Logic.Upload
{
    public class LUploadStrategyInspectionImages : LUploadStrategyBase
    {
        public LUploadStrategyInspectionImages(EXmlOutboxBase outbox) : base(outbox)
        {


        }

        public override void StartWork(BackgroundWorker worker, DoWorkEventArgs workerEvents)
        {
            List<EInspectionImage> _images = LInspectionImage.GetAll(m_OutboxDatabase.ConnectionString);
            Int32 _TotalToUpload = _images.Count;
            Int32 _CountUploaded = _TotalToUpload;

            WebUploaderHelper _UploaderHelper = new WebUploaderHelper(Constants.K_WEBSITEUPLOADER);
            foreach (EInspectionImage image in _images)
            {
                #region Cancel Process
                if (worker.CancellationPending)
                {
                    workerEvents.Cancel = true;
                    return;
                }
                #endregion

                if (image.ServerID == Int32.MinValue)
                {
                    #region Uploading Image
                    try
                    {
                        image.ServerID = LInspectionImage.Save(image);
                        string _FileName = image.Inspection.ServerID + "_" + image.ServerID + ".jpg";
                        string _FolderName = DirHelper.GetImageDirectory(image.Inspection.ServerID, image.ServerID);
                        if (image.ImageFile.Exists)
                            _UploaderHelper.Upload(_FolderName, _FileName, image.ImageFile, Enum.GetName(typeof(DatabaseTypes), m_OutboxDatabase.DatabaseType));
                    }
                    catch (Exception ex)
                    {
                        RegisterError.SaveError(ex);
                        m_HasErrors = true;
                    }
                    finally
                    {
                        LInspectionImage.UpdateUploadedData(image, m_OutboxDatabase.ConnectionString);
                        _CountUploaded--;
                        worker.ReportProgress(ReportProgress(_CountUploaded, _TotalToUpload));
                    } 
                    #endregion
                }
                System.Threading.Thread.Sleep(100);
                #region Cancel Process
                if (worker.CancellationPending)
                {
                    workerEvents.Cancel = true;
                    return;
                }
                #endregion

            }
        }

        public override void FinishWork()
        {
            if(!m_HasErrors)
            {
                LXmlOutbox _serviceLogic = new LXmlOutbox(m_OutboxDatabase);
                _serviceLogic.CopyToSentFolder();

            }
        }
    }
}
